<?php
/**
 * User: hjd-yao
 * Date: 2019/7/5
 * Time: 17:07
 */

namespace Shop\Http\Middleware;


use App\Models\Shop\ShopCartModel;
use Closure;
use Frontend\Models\UserBaseModel;
use Illuminate\Http\Request;

class UserLoginData
{
    public function handle(Request $request, Closure $next)
    {
        if(UserBaseModel::init()->isLogin()){
            $this->loginData();
        }
        $this->showLevel();
        return $next($request);
    }

    public function loginData()
    {
        view()->share('lgInfo',UserBaseModel::init()->loginInfo());
        \request()->offsetSet('user_id',UserBaseModel::init()->loginInfo('member_id'));

        $user_id = \request()->input('user_id');

        $list = ShopCartModel::with('goods')->where('user_id',$user_id)->get()->toArray();
        $num = count($list);
        view()->share('cart_num',$num);
        view()->share('cart_goods',$list);
    }

    public function showLevel(){

        if(request()->session()->has('user')){
            $user_auth = request()->session()->get('user');
            $level = UserBaseModel::where('member_id',$user_auth['lg_info']['member_id'])->value('level');
        }else{
            $level = 0;
        }
        view()->share('level',$level);
    }
}